﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Globalization;
using FI.Core.ApplicationContexts;

namespace Sample.Common
{
    public class AppContext
    {
        private const string UserIdKey = "___UserId";
        private const string IsAnonymousKey = "___IsAnonymous";
        private const string NickNameKey = "___NickName";

        private const string RolesKey = "___Roles";
        public static readonly string WebAppName = "__xxx_Web";
        public static readonly string ServiceAppName = "__xxx_Service";
        public static readonly string IsIntranetKey = "__IsIntranet";
        public static readonly string AccessLevelKey = "__xxx_AccessLevel";

        private static readonly AppContext CurrentInstance = new AppContext();

        public static AppContext Current
        {
            get { return CurrentInstance; }
        }

        #region Properties

        public string DbTransactionId
        {
            get { return ApplicationContext.Current.TransactionId; }
            set { ApplicationContext.Current.TransactionId = value; }
        }

        public CultureInfo UICulture
        {
            get { return ApplicationContext.Current.UICulture; }
            set
            {
                ApplicationContext.Current.UICulture = value;
                ApplicationContext.Current.Culture = value;
            }
        }
 

        public string UserId
        {
            get
            {
                var userId = ApplicationContext.Current.UserId;
                if (userId == null)
                {
                    return string.Empty;
                }


                return userId;
            }
            set { ApplicationContext.Current.UserId = value; }
        }

        public string UserName
        {
            get { return ApplicationContext.Current.UserName; }
            set { ApplicationContext.Current.UserName = value; }
        }

      
 


        public string NickName
        {

            get
            {
                var data = ApplicationContext.Current.GetValue<string>(NickNameKey);
                return data;
            }
            set
            {
                ApplicationContext.Current.SetContext(NickNameKey, value);
            }
        }
       

        public bool IsAuthenticated
        {
            get { return IsNotAnonymous && !string.IsNullOrEmpty(UserName); }
        }

        public int AccessLevel
        {
            get
            {
                var data = ApplicationContext.Current.GetValue<int>(AccessLevelKey);
                return data;
            }
            set
            {
                ApplicationContext.Current.SetContext(AccessLevelKey, value);
            }
        }

        public bool IsNotAnonymous
        {
            get
            {
                var data = ApplicationContext.Current.GetValue<bool>(IsAnonymousKey);
                return data;
            }
            set { ApplicationContext.Current.SetContext(IsAnonymousKey, value); }
        }

        public string[] Roles
        {
            get
            {
                if (HttpContext.Current != null
                    && HttpContext.Current.Session != null)
                {
                    var data = HttpContext.Current.Session[RolesKey] as string[];
                    return data;
                }
                return null;
            }
            set
            {
                if (HttpContext.Current != null
                    && HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session[RolesKey] = value;
                }

            }
        }


        #endregion



    }

}
